package  
{
	import events.GCEvent;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import org.flixel.data.FlxKeyboard;
	import ui.textfield.GCStylizedText;
	import ui.textfield.GCTextField;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	import org.flixel.FlxG;
	import ui.button.GCButton;
	/**
	 * A screen to allow the user to pick a user name that other player's
	 * will be able to see. Will only show up for brand new players.
	 */
	public class GCLoginScreen extends Sprite
	{
		private static const MENU_LABEL:String = "Welcome to Free Trader!"
		private static const LOGIN_MESSAGE:String = "Select a user name that other players will be able to see!";
		
		private var _welcomeText:GCStylizedText;
		private var _descriptionText:GCTextField;
		private var _inputArea:TextField;
		private var _inputFormat:TextFormat;
		
		private var _confirmButton:GCButton;
		private var _imageScreen:Sprite;
		
		public function GCLoginScreen() 
		{
			_inputArea = new TextField();
			_inputFormat = new TextFormat();
			_descriptionText = new GCTextField();
			_imageScreen = new Sprite();
			
			_confirmButton = new GCButton(onConfirmName);
			
			init();
		}
		
		private function init():void
		{
			var bg:Bitmap = GCResources.getResource("worldMap");
			bg.height = FlxG.height;
			this.addChild(bg);
			
			_welcomeText = new GCStylizedText();
			_welcomeText.setBgStyle(GCStylizedText.STYLE_ONE, 200, 50);
			_welcomeText.text = MENU_LABEL;
			_welcomeText.x = this.width / 2 - _welcomeText.width / 2;
			_welcomeText.y = 100;
			this.addChild(_welcomeText);
			
			_descriptionText.text = LOGIN_MESSAGE;
			_descriptionText.setStyle(GCTextField.TEXT_TYPE_TITLE);
			_descriptionText.setDimensions(200, 50);
			_descriptionText.x = this.width / 2 - _descriptionText.width / 2;
			_descriptionText.y = _welcomeText.y + _welcomeText.height;
			this.addChild(_descriptionText);
			
			_inputArea.type = TextFieldType.INPUT;
			_inputArea.background = true;
			_inputArea.width = 200;
			_inputArea.height = 30;
			_inputArea.x = this.width / 2 - _inputArea.width / 2;
			_inputArea.y = _descriptionText.y + _descriptionText.height;
			_inputArea.addEventListener(KeyboardEvent.KEY_DOWN, onConfirmNameKeyboard);
			
			_inputFormat.bold = true;
			_inputFormat.size = 20;
			_inputFormat.font = "Arial";
			_inputArea.defaultTextFormat = _inputFormat;
			
			this.addChild(_inputArea);
			
			_confirmButton.loadGraphic(GCResources.plate, 200, 30);
			_confirmButton.loadText("Submit Name");
			_confirmButton.x = this.width / 2 - _confirmButton.width / 2;
			_confirmButton.y = _inputArea.y + _inputArea.height;
			
			this.addChild(_confirmButton);
			
			var stall:Bitmap = GCResources.getResource("Spice Stall");
			var mill:Bitmap = GCResources.getResource("Timber Mill");
			mill.x = stall.x + stall.width;
			var farm:Bitmap = GCResources.getResource("Farm");
			farm.x = mill.x + mill.width;
			_imageScreen.addChild(stall);
			_imageScreen.addChild(mill);
			_imageScreen.addChild(farm);
			
			_imageScreen.x = 150;
			_imageScreen.y = _confirmButton.y + _confirmButton.height + 50;
			
			this.addChild(_imageScreen);
		}
		
		public function get userNameSelected():String
		{
			return _inputArea.text;
		}
		
		private function onConfirmName(event:MouseEvent):void
		{
			if (_inputArea.text == "")
			{
				// If blank, require them to select a different name
			}
			else
			{
				this.dispatchEvent(new Event(GCEvent.SELECTED_USER_NAME_EVENT));
			}
		}
		
		private function onConfirmNameKeyboard(event:KeyboardEvent):void
		{
			
		}
	}

}